/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO.InformationFilter;

import Info.InterestingInfo;
import DAO.DAOAccess;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 *
 * @author tran tuan tai
 */
public class InterestingDAO
{
      public static void DoEmpty()
    {
         try
        {
            java.sql.Connection conn = DAOAccess.GetConnection();
            String sql = "DELETE FROM INTERESTING";
            PreparedStatement stat = conn.prepareCall(sql);
            stat.execute();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public int Insert(InterestingInfo info)
    {
        int id = -1;
        try
        {
            java.sql.Connection conn = DAOAccess.GetConnection();
            String sql = "INSERT INTO INTERESTING(USER, PERIOD, CATEGORY, INTERESTINGSCORE) VALUES(?, ?, ?, ?)";

            PreparedStatement statement = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
            statement.setInt(1, info.User);
            statement.setInt(2, info.Period);
            statement.setInt(3, info.Category);
            statement.setDouble(4, info.InterestingScore);

            statement.executeUpdate();
            ResultSet result = statement.getGeneratedKeys();
            result.first();
            id = result.getInt(1);
            statement.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

        return id;
    }



    public Double GetScore(int userID, int category, int period)
    {
        String sql = "SELECT INTERESTINGSCORE FROM INTERESTING WHERE USER = ? AND PERIOD = ? AND CATEGORY = ?";
        Connection conn = DAOAccess.GetConnection();
        try
        {
            PreparedStatement stat = conn.prepareStatement(sql);
            stat.setInt(1, userID);
            stat.setInt(2, period);
            stat.setInt(3, category);

            stat.executeQuery();
            ResultSet rs = stat.getResultSet();
            rs.beforeFirst();

            while (rs.next())
            {
                return rs.getDouble(1);
            }

        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

        return null;
    }
}
